package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;


    //分页查询员工信息
   /* @Override
    public PageBean empInfo(Integer page, Integer pageSize) {
        //总记录数
        Integer count = empMapper.count();
        //起始索引为要查询的页码数 - 1 * 每页展示的记录数
        Integer start = (page - 1) * pageSize;
        //每页展示的员工数据
        List<Emp> empList = empMapper.empList(start,pageSize);

        //传递展示的页码和记录数
        PageHelper.startPage(page,pageSize);
        List<Emp> empList= empMapper.empList();
        //Page中封装的是结果信息
        Page<Emp> p = (Page<Emp>) empList;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }*/

    //分页条件查询员工信息
    @Override
    public PageBean empInfo2(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        PageHelper.startPage(page, pageSize);//设置分页参数
        List<Emp> empList = empMapper.empWhereList(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;//获取分页结果
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());//封装pageBean对象
        return pageBean;
    }

    //批量删除员工
    @Override
    public void deleteEmp(List<Integer> ids) {
        empMapper.deleteEmp(ids);
    }

    //添加员工
    @Override
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
    }

    //根据id查询员工
    @Override
    public Emp selectEmpById(Integer id) {
        return empMapper.selectEmpById(id);
    }

    //修改员工信息
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }

    //登录
    @Override
    public Emp login(Emp emp) {
        return empMapper.selectEmpByUsernameAndPassword(emp);
    }
}
